package com.org.khtn.command.select;

import java.util.Map;
import java.util.Vector;

import com.google.common.collect.Multimap;
import com.org.khtn.command.MyQuery;
import com.org.khtn.data.Data;
import com.org.khtn.data.DataTables;
import com.org.khtn.data.Manager;

public class SelectQuery extends MyQuery {
	final String className = getClass().getSimpleName();
	
	public SelectQuery(String Query, String DatabaseName) {
		dt = new DataTables(new Data(DatabaseName, "sadsa", "mytest\\data\\"));
		query = Query;
		parse = new ParseSelect(Query);
		databaseName =DatabaseName;
	}

	@Override
	public Map<String, Multimap<String, String>> executeReader() {
		// TODO Auto-generated method stub
		final String methodName = "executeReader():";
		System.out.println(className + methodName + "vao: 1");

		if (!parse.eval())
			return null;
		else
			key = parse.parse();
		System.out.println(className + methodName + "vao: 2");

		From a = new From();
		a.setData(dt.getData());
		a.databaseName = databaseName;
		Vector v = dt.getAllTableName();
		System.out.println(className + methodName + "v:" + v);
		int length = v.size();
		System.out.println(className + methodName + "length:" + length);

		String[] tam123 = new String[length];
		for (int i = 0; i < length; i++) {
			tam123[i] = (String) v.get(i);
			a.putTableDefine(tam123[i], dt.getTableDefines(tam123[i]));
			System.out.println(className + methodName + "tam123:" + tam123[i]);

		}
		a.setTables(tam123);
		for (int i = 0; i < 8; i++) {
			if (key[i] != null)
				if (key[i].matches("\\(.*\\)")) {
					System.out.println("Aaa :" + key[i]);
					key[i] = key[i].substring(1, key[i].length() - 1);
				}
			System.out.println(key[i]);

		}
		if (key[5] !=null)
		{
			String[] tam1 = key[5].split(",");
			String[] tam2 = key[0].split(",");
		}
		return a.from(key[1]).join(key[2], key[3]).where(key[4]).groupBy(key[5]).select(key[0]);
	}

	public static void main(String[] args) {
		SelectQuery mp = new SelectQuery(
				"select * from hocsinh",
				"qlhs");
		System.out.println("sadsa: " + mp.executeReader());
	}
}
